﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;

namespace AllInOnePluginBL.FileHostingExtractors
{
    public class VideoweedExtractor : IFileHostingExtractor
    {
        #region Private Members
        private const string INFO_URL_FORMAT = "http://www.videoweed.es/api/player.api.php?file={0}&key={1}&pass=undefined";
        private const string URL_REGEX = "<a href=\"http://www.videoweed.es/file/(.+?)\" target=\"_blank\">";
        private const string URL_FORMAT = "http://www.videoweed.es/file/{0}"; 
        #endregion

        #region Implementation of IFileHostingExtractor
        public List<string> GetMovieUrls(string content)
        {
            var movieUrls = new List<string>();
            var exp = new Regex(URL_REGEX, RegexOptions.IgnoreCase);
            var matches = exp.Matches(content);
            foreach (Match match in matches)
            {
                var movieUrl = string.Format(URL_FORMAT, match.Groups[1].Value);
                if (!movieUrls.Contains(movieUrl))
                {
                    movieUrls.Add(movieUrl);
                }
            }
            return movieUrls;
        }

        public VideoResult ExtractMovieUrl(string movieUrl)
        {
            var genericWebRequest = new GenericWebRequest();
            var content = genericWebRequest.GetResponse(movieUrl);
            //var content = Utils.GetPage(movieUrl, Encoding.UTF8);

            const string VideoNameExp = "<meta name=\"title\" content=\"(.+?)\" /> ";
            const string FileExp = "flashvars.file=\"(.+?)\";";
            const string KeyExp = "flashvars.filekey=\"(.+?)\";";

            var videoName = GetStringFromRegularExpression(content, VideoNameExp);
            var file = GetStringFromRegularExpression(content, FileExp);
            var key = GetStringFromRegularExpression(content, KeyExp);

            var fullInfoUrl = string.Format(INFO_URL_FORMAT, file, key);
            genericWebRequest.RequestCookies = genericWebRequest.ResponseCookies;
            var infoContent = genericWebRequest.GetResponse(fullInfoUrl, "http://www.videoweed.es/player/weedplayerv3.swf");
            //var infoContent = Utils.GetPage(fullInfoUrl, Encoding.UTF8);

            infoContent = Utils.LoopDecodeUrl(infoContent);

            var urlStartPos = infoContent.IndexOf("=");
            if(urlStartPos == -1 || urlStartPos == 0)
                throw new Exception("Cant find movie start url");

            return new VideoResult(infoContent.Substring(4), videoName, true, genericWebRequest.ResponseCookies);
        }

        public bool IsContainVideoLink(string content)
        {
            var match = Regex.Match(content.ToLower(), "videoweed", RegexOptions.IgnoreCase);
            return match.Success;
        }
        #endregion

        #region Private Methods
        private string GetStringFromRegularExpression(string pageContent, string regEx)
        {
            var match = Regex.Match(pageContent, regEx, RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                throw new Exception(string.Format("Cant find {0}", regEx));
            }
            return match.Groups[1].Value;
        } 
        #endregion
    }
}
